function openaddressdiv(){
	$("#showAddressBlock").addClass("st-target");
}
function colseaddressdiv(){
	$("#showAddressBlock").removeClass("st-target");
}
$("#addcontact").click(function(){
	var contactid = $("#contactid").val();
	var postdata = $('#formcontact').serializeArray();
	var postdatagroup = {};
	$.each(postdata, function(i, field){
		postdatagroup[field.name] = field.value;
	});
	$.post("/portal/Ajax/index.html",{ajaxmember:'ok',ajaxname:'addcontact',ajaxid:contactid,postdata:postdatagroup},function(data){
		var data=eval('('+data+')');
		console.log(data);
		if(data.error == 0)
		{
			var content = data.content;
			if(content.defok == 1){
				$(".contactlist .contact_form").each(function(){  
					 $(this).removeClass("cur");  
				});
			}
			if(contactid != "")
			{
				$("#contactaddress"+contactid).find($(".name")).html(content.name);
				$("#contactaddress"+contactid).find($(".tel")).html(content.tel);
				$("#contactaddress"+contactid).find($(".address")).html(content.address);
				if(content.defok == 1){
					$("#contactaddress"+contactid).addClass("cur");
				}
			}else{
				if(content.defok == 1){
					var curok = "cur";
				}
				$(".contactlist").append('<div class="contact_form '+curok+'" data-id="563"><i class="delete">×</i><h3>联系人姓名:<span>'+content.name+'</span></h3><p class="first">联系电话:<span>'+content.tel+'</span></p><p><label>联系地址:</label><span>'+content.address+'</span></p><a href="javascript:;" class="add-select" style="right:80px;">选择</a><a href="javascript:;" class="add-edit">修改</a><a href="javascript:;" class="def-select">设置默认</a></div>');
				
				
				// 添加列表收货地址
				$(".addr-list li").each(function(){
					$(".addr-list li").removeClass("check");
				});
				$(".addr-list ul").append('<li id="selectaddress'+content.id+'" class="check" data-id="'+content.id+'" data-defok="'+content.defok+'"><dl class="add-select"><dt class="name">'+content.name+'</dt><dd class="address"> '+content.address+'</<dd class="tel">'+content.tel+'</</<div class="addr-opration addr-edit add-edit"><a href="javascript:;" class="addr-edit-btn" onclick="openaddressdiv();"><svg class="icon icon-edit"><use xlink:href="#icon-edit"></use></svg></a></div><div class="addr-opration addr-del delete"><a href="javascript:;" class="addr-del-btn"><svg class="icon icon-del"><use xlink:href="#icon-del"></use></svg></a></div></li>');
				$("#selectaddress").val(content.id);
				
				// 关闭收货地址层
				colseaddressdiv()
			}
		}
		
		Wind.use("noty", function () {
			noty({
				text: data.message,
				type: "success",
				layout: "center"
			});
		});
		
	});	
});
$(".add-edit").click(function(){
	stylePlugins.endMaoPao();
	var tinfo = $(this).parent();
	$("#addname").val(tinfo.find($(".name")).html());	
	$("#addtel").val(tinfo.find($(".tel")).html());	
	$("#addaddress").text(tinfo.find($(".address")).html());	
	$("#contactid").val(tinfo.data("id"));
	if(tinfo.data("defok") == 1){
		$("#adddefault1").prop("checked",true);
		$("#adddefault2").prop("checked",false);
	}else{
		$("#adddefault1").prop("checked",false);
		$("#adddefault2").prop("checked",true);
	}
});
$(".add-new").click(function(){
	stylePlugins.endMaoPao();
	$("#addname").val("");	
	$("#addtel").val("");	
	$("#addaddress").text("");	
	$("#contactid").val("");
	$("#adddefault1").prop("checked",false);
	$("#adddefault2").prop("checked",true);
});
$(".delete").click(function(){
	stylePlugins.endMaoPao();
	if(confirm("确实要删除吗？")){
		var contactid = $(this).parent().data("id");
		$.post("/portal/Ajax/index.html",{ajaxmember:'ok',ajaxname:'delcontact',ajaxid:contactid},function(data){
			var data=eval('('+data+')');
			console.log(data);
			
			$("#contactaddress"+contactid).remove();
			Wind.use("noty", function () {
				noty({
					text: data.message,
					type: "success",
					layout: "center"
				});
			});
		});	
	}
});
$(".add-select").click(function(){
	stylePlugins.endMaoPao();
	var contactid = $(this).parent().data("id");
		
	$(".addr-list li").each(function(){  
		 $(this).removeClass("check");  
	});
	$("#selectaddress"+contactid).addClass("check");
	$("#selectaddress").val(contactid);
	colseaddressdiv();

});
$(".def-select").click(function(){
	stylePlugins.endMaoPao();
	if(confirm("确实要设置为默认地址吗？")){
		var contactid = $(this).parent().data("id");
		$.post("/portal/Ajax/index.html",{ajaxmember:'ok',ajaxname:'defcontact',ajaxid:contactid},function(data){
			var data=eval('('+data+')');
			console.log(data);
			
			if(data.error == 0){
				$(".contactlist .contact_form").each(function(){  
					 $(this).removeClass("cur");  
				});
				$("#contactaddress"+contactid).addClass("cur");
				
				Wind.use("noty", function () {
					noty({
						text: "设置成功",
						type: "success",
						layout: "center"
					});
				});
			}
		});	
	}
});